//
//  KATNavBar.m
//  KATFramework
//
//  Created by Yi Yu on 2017/5/27.
//  Copyright © 2017年 KatApp. All rights reserved.
//

#import "KATNavBar.h"
#import "KATRouter.h"



@interface KATNavBar ()

///是否显示中
@property(nonatomic,assign) BOOL isShown;

///返回按钮
@property(nonatomic,retain) KATButton *backButton;

///图标
@property(nonatomic,retain) KATSprite *image;

///标题
@property(nonatomic,retain) KATLabel *label;


@end



@implementation KATNavBar


//获取实例
+ (instancetype)navBar
{
    return [[[self alloc] init] autorelease];
}


//初始化
- (instancetype)init
{
    self=[super init];
    
    self.padding=(MIN(SCREEN_WIDTH, SCREEN_HEIGHT))*0.02;
    self.height=44.0;//(MIN(SCREEN_WIDTH, SCREEN_HEIGHT))*0.14;
    
    self.backgroundColor=KCOLOR_CLEAR;
    self.layer.borderWidth=0.5;
    self.layer.borderColor=KCOLOR_LINE.CGColor;
    
    //模糊层
    self.blurView=[[[UIVisualEffectView alloc] init] autorelease];
    self.blurView.effect=[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    [self addSubview:self.blurView];
    
    //返回按钮
    self.backButton=[KATButton buttonWithFrame:CGRectZero type:KATButtonTypeLeft color:KCOLOR_NIGHT lineWidth:1.5 scale:1.0 andOnClickAction:nil];
    [self addSubview:self.backButton];
    
    //图标
    self.image=[KATSprite sprite];
    [self addSubview:self.image];
    
    //标题
    KATTextStyle *tStyle=[KATTextStyle textStyle];
    
    tStyle.textFont=[UIFont systemFontOfSize:(self.height-2*self.padding)*0.62];
    tStyle.textColor=KCOLOR_NIGHT;
    tStyle.hasDateStyle=NO;
    tStyle.textAlignment=NSTextAlignmentCenter;
    tStyle.marginTop=1;
    tStyle.marginLeft=1;
    tStyle.marginRight=1;
    tStyle.marginBottom=1;
    
    self.label=[KATLabel labelWithFrame:CGRectZero andStyle:tStyle];
    [self addSubview:self.label];
    
    self.color=KCOLOR_NIGHT;
    self.button=nil;
    self.icon=nil;
    self.title=nil;
    self.hasBackButton=YES;
    self.hasAnimation=YES;
    self.animationDuration=0.12;
    
    self.isShown=NO;
    
    self.opacity=1.0;
    
    //尺寸
    self.bounds=BOUNDS(SCREEN_WIDTH+self.layer.borderWidth*2, STATUS_BAR_HEIGHT+self.height);
    self.center=POINT(SCREEN_WIDTH*0.5, -self.bounds.size.height*0.5-self.layer.borderWidth);
    
    [self update];
    
    return self;
}



- (void)setColor:(UIColor *)color
{
    if(color!=_color)
    {
        [_color release];
        _color=[color retain];
        
        [self update];
    }
}


- (void)setTitle:(NSString *)title
{
    if(title!=_title)
    {
        [_title release];
        _title=[title retain];
        
        [self update];
        
        if(title)
        {            
            if(self.opacity>=1.0)
            {
                [_label clearAnimations];
                [_label blinkWithAlpha:0.6 duration:0.12 repeat:1];
            }
        }
    }
}


- (void)setIcon:(UIImage *)icon
{
    if(icon)
    {
        if(icon.size.width<=0 || icon.size.height<=0)
        {
            icon=nil;
        }
    }
    
    if(icon!=_icon)
    {
        [_icon release];
        _icon=[icon retain];
        
        if(self.opacity>=1.0)
        {
            [_image.layer addAnimation:[KATSprite transitionWithType:@"fade" subtype:nil duration:0.12 delay:0] forKey:nil];
        }
        
        [self update];
    }
}


- (void)setButton:(KATButton *)button
{
    if(button!=_button)
    {
        [_button removeFromSuperview];
        
        [_button release];
        _button=[button retain];
        
        [self addSubview:_button];
        
        [self update];
    }
}


- (void)setHasBackButton:(BOOL)hasBackButton
{
    _hasBackButton=hasBackButton;
    
    [self update];
}


- (void)setOpacity:(float)opacity
{
    if(opacity<0.0)
    {
        opacity=0.0;
    }
    
    if(opacity>1.0)
    {
        opacity=1.0;
    }
    
    _opacity=opacity;
    
    _backButton.alpha=_opacity;
    _button.alpha=_opacity;
    _label.alpha=_opacity;
    _image.alpha=_opacity;
}


//更新内容
- (void)update
{    
    //尺寸
    self.bounds=BOUNDS(SCREEN_WIDTH+self.layer.borderWidth*2, STATUS_BAR_HEIGHT+_height);
//    self.frame=RECT(-self.layer.borderWidth, _isShown?-self.layer.borderWidth:-self.bounds.size.height-self.layer.borderWidth, self.bounds.size.width, self.bounds.size.height);
    
    //模糊效果
    _blurView.frame=self.bounds;
    
    //返回按钮
    _backButton.frame=RECT(self.layer.borderWidth+((ORIENTATION_LANDSCAPE && DEVICE_IPHONE_X)?_height:0.0), STATUS_BAR_HEIGHT, _height, _height);
    _backButton.color=_color;
    _backButton.scale=_height>0?(_height-2*_padding)/_height*0.7:1.0;
    
    
    //是否隐藏返回按钮
    if(_hasBackButton)
    {
        _backButton.hidden=NO;
    }
    else
    {
        _backButton.hidden=YES;
    }
    
    //右按钮
    if(_button)
    {
        _button.frame=RECT(self.bounds.size.width-self.layer.borderWidth-_height-((ORIENTATION_LANDSCAPE && DEVICE_IPHONE_X)?_height:0.0), STATUS_BAR_HEIGHT, _height, _height);
        _button.color=_color;
        _button.lineWidth=_backButton.lineWidth;
        _button.scale=_backButton.scale;
    }
    
    //计算尺寸
    float imageHeight=_height-2*_padding;
    float labelHeight=_height-2*_padding;
    float labelWidth=self.bounds.size.width-(_height+_padding*0.5)*2;
    
    //标题图标
    _image.contents=_icon;
    
    _label.text=_title;
    _label.style.textColor=_color;
    
    //位置尺寸
    if(_icon && _title)//图标和标题都有
    {
        [_label resizeFrame];
        
        float titleWidth=_label.bounds.size.width;
        float imageWidth=_icon.size.width/_icon.size.height*_height;
        
        if(titleWidth+imageWidth>labelWidth)//超出最大宽度
        {
            //图片最多占一半尺寸
            if(imageWidth>labelWidth*0.5)
            {
                imageWidth=labelWidth*0.5;
            }
            
            titleWidth=labelWidth-imageWidth;
        }
        
        _image.frame=RECT((self.bounds.size.width-(imageWidth+titleWidth))*0.5, STATUS_BAR_HEIGHT+_padding, imageWidth, imageHeight);
        _label.frame=RECT((self.bounds.size.width-(imageWidth+titleWidth))*0.5+imageWidth, STATUS_BAR_HEIGHT+_padding, titleWidth, labelHeight);
        
        _image.hidden=NO;
        _label.hidden=NO;
    }
    else if(_icon)//只有图标
    {
        _image.frame=RECT((self.bounds.size.width-labelWidth)*0.5, STATUS_BAR_HEIGHT+_padding, labelWidth, imageHeight);
        
        _image.hidden=NO;
        _label.hidden=YES;
    }
    else if(_title)//只有标题
    {
        _label.frame=RECT((self.bounds.size.width-labelWidth)*0.5, STATUS_BAR_HEIGHT+_padding, labelWidth, labelHeight);
        
        _label.hidden=NO;
        _image.hidden=YES;
    }
    else//都没有
    {
        _image.hidden=YES;
        _label.hidden=YES;
    }
}


//显示
- (void)show
{
    [self update];
    
    if(!_isShown)
    {
        _isShown=YES;
        
        self.hidden=NO;
        
        if(_hasAnimation)
        {
            [UIView animateWithDuration:_animationDuration animations:^
            {
                self.center=POINT(SCREEN_WIDTH*0.5, self.bounds.size.height*0.5-self.layer.borderWidth);
            }];
        }
        else
        {
            self.center=POINT(SCREEN_WIDTH*0.5, self.bounds.size.height*0.5-self.layer.borderWidth);
        }
    }
    else
    {
        self.center=POINT(SCREEN_WIDTH*0.5, self.bounds.size.height*0.5-self.layer.borderWidth);
    }

}


//隐藏
- (void)hide
{
    [self update];
    
    if(_isShown)
    {
        _isShown=NO;
        
        if(_hasAnimation)
        {
            [UIView animateWithDuration:_animationDuration animations:^
            {
                self.center=POINT(SCREEN_WIDTH*0.5, -self.bounds.size.height*0.5-self.layer.borderWidth);
            }
            completion:^(BOOL finished)
            {
                self.hidden=YES;
            }];
        }
        else
        {
            self.center=POINT(SCREEN_WIDTH*0.5, -self.bounds.size.height*0.5-self.layer.borderWidth);
            
            self.hidden=YES;
        }
    }
    else
    {
        self.center=POINT(SCREEN_WIDTH*0.5, -self.bounds.size.height*0.5-self.layer.borderWidth);
        
        self.hidden=YES;
    }
}


//释放内存
- (void)dealloc
{
    [_color release];
    [_backButton release];
    [_blurView release];
    [_icon release];
    [_title release];
    [_button release];
    [_image release];
    [_label release];
    
    [super dealloc];
}


@end
